/*
 * Created on Jun 28, 2004
 *
 */
package littlecyberwar.tool;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;

import littlecyberwar.ui.*;
import littlecyberwar.model.*;

import littlecyberwar.util.*;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class MouseAlignHorizontalHandler extends MultiSelectMouseHandler {
	
	public MouseAlignHorizontalHandler() {
		super();
		id = H_ALIGNHORIZONTAL;
		unitSelectedText = "Click on a selected unit to have other selected units align horizontally to it.";
		noUnitSelectedText = "Drag Select or shift-click select units you wish to align horizontally.";
	}
	/**
	 * called here when a group of elements are selected
	 * The first element in the array contains the reference element
	 * 
	 * 1. Sort all the selected elements by horizontal coordinates
	 * 2. Iterates from reference element all the way to the "left", and then calculate
	 * 		the new trandform for that unit
	 * 3. Iterates from reference element all the way "right", and then calculate new transform
	 * 4. redraw all the element area (old and new).
	 * 
	 */
	public void startAction( MouseEvent e ) {
		super.startAction(e);
	  	ArrayList sortList = createLeftFrontList( selectedElements );
		GroupSortElementHolder refCont = (GroupSortElementHolder)sortList.get(0);		
	  	Collections.sort(sortList);
		
		// now find the index of the reference element in the new sorted list
		int refIndex = sortList.indexOf(refCont);
		if ( refIndex != -1 ){
			// Iterates from reference element all the way to the "left"
			AffineTransform trans = refCont.cont.element.getTransform();
			
			for ( int i=refIndex-1; i >= 0; i-- ){				 	
				GroupSortElementHolder sortHolder = (GroupSortElementHolder)sortList.get(i);
				Element el = sortHolder.cont.element;
				double currWidth = el.getWidth();
				trans = (AffineTransform)trans.clone();
				trans.translate(-currWidth, 0);
				sortHolder.cont.currTrans = trans;
				Shape originalRec = sortHolder.cont.element.getRectangle();
				sortHolder.cont.selectedRectangle = trans.createTransformedShape(originalRec);
//				Area a = new Area(el.getFinalShape().getBounds());				
//				selectedArea.add(a);
				DirtyAreaManager.addDirtyArea(el.getFinalShape());
			}
			
			// Iterates from reference element all the way "right"
			trans = refCont.cont.element.getTransform();
			double lastWidth = refCont.cont.element.getWidth();
			int limit = sortList.size();
			
			for ( int i=refIndex+1; i < limit; i++ ){				 	
				GroupSortElementHolder sortHolder = (GroupSortElementHolder)sortList.get(i);
				Element el = sortHolder.cont.element;
				double currWidth = el.getWidth();
				trans = (AffineTransform)trans.clone();
				trans.translate(lastWidth, 0);
				sortHolder.cont.currTrans = trans;
				Shape originalRec = sortHolder.cont.element.getRectangle();
				sortHolder.cont.selectedRectangle = trans.createTransformedShape(originalRec);
				lastWidth = currWidth;
//				Area a = new Area(el.getFinalShape().getBounds());				
//				selectedArea.add(a);
				
				DirtyAreaManager.addDirtyArea(el.getFinalShape());
			}
//			Rectangle rec = selectedArea.getBounds();			
//			rec.grow(PlayingBoardPanel._DEFAULT_STROKE_WIDTH, PlayingBoardPanel._DEFAULT_STROKE_WIDTH);
//			comp.repaint(rec);
			
			DirtyAreaManager.checkAndRepaint(comp);
			// stopAction();
		}
//		Debug.out("after Horizontalhandler startAction: currNum of selected elements = " + selectedElements.size() );
		return;
	}

	public UnitCommand stopAction() {
	  // Debug.out("stopAction(): mouseReleased");
	  // Debug.out("stopAction: currNum of selected elements = " + selectedElements.size() );
	  MultiUnitCommand multCmd = null;
	  if ( selectedElements.size() > 1){
		  multCmd = new MultiUnitCommand();  	
	  }
	  UnitCommand cmd=null;
	  for ( int i=0; i< selectedElements.size(); i++){
		  SelectedElementContainer elcont = (SelectedElementContainer)selectedElements.get(i);
		  Element el = elcont.element;
		
		  if ( elcont.selectedRectangle != null ) {
			  // Debug.out("el.getElementId()= " + el.getElementId());
			  cmd = new UnitCommand(el.getElementId(), id, elcont.currTrans, pixMoved/mapScale);		
			  cmd.setPlayerName(Players.getSelfName());
			  if ( multCmd != null ){
				  multCmd.addUnitCommand(cmd);
			  }
		  } else {
			  Debug.out("( elcont.selectedRectangle == null )");
		  }
			  // clean up container
//		elcont.resetElementContainer();
	  }
	  if (multCmd != null ){
		  cmd = multCmd;
	  }
	  // Debug.out("end of stopAction: currNum of selected elements = " + selectedElements.size() );
	  return ( cmd );      
	}

}
